#include "FrogEnemyObject.h"
#include "include.h"

#define TIGHT_TONGUE 1000.0
#define LOOSE_TONGUE 200.0
#define TONGUE new Beard(&tongueAnchor,5, 0.1, 0.01, TIGHT_TONGUE, 1,0,0)

FrogEnemyObject::FrogEnemyObject(TextureManager * textureManager,
						 Vector2D position, 
						 float mass,
						 Object * target,
						 Weapon * weapon)
:CpuControlledObject(textureManager, position, mass,NULL,target,weapon, FROG_SIZE, FROG_TEXTURE, PANIC_FROG_TEXTURE) 
{
	canUseTongue = true;
	tongueAnchorOffset = Vector2D(0.0, 0.0);
	tongueAnchor = position + tongueAnchorOffset;
	tongueString = TONGUE;
}
FrogEnemyObject::FrogEnemyObject( TextureManager * textureManager,
						 Vector2D position, 
						 float mass,
						 Vector2DList * waypoints,
						 Object * target)
:CpuControlledObject(textureManager, position, mass,waypoints,target,NULL, FROG_SIZE,FROG_TEXTURE, PANIC_FROG_TEXTURE) 
{
	canUseTongue = true;
	tongueAnchorOffset = Vector2D(0.0, 0.0);
	tongueAnchor = position + tongueAnchorOffset;
	tongueString = TONGUE;
}
FrogEnemyObject::FrogEnemyObject( TextureManager * textureManager,
						 Vector2D position, 
						 float mass,
						 Object * target)
:CpuControlledObject(textureManager, position, mass,NULL,target,NULL, FROG_SIZE,FROG_TEXTURE, PANIC_FROG_TEXTURE) 
{
	canUseTongue = true;
	tongueAnchorOffset = Vector2D(0.0, 0.0);
	tongueAnchor = position + tongueAnchorOffset;
	tongueString = TONGUE;
}

void FrogEnemyObject::InitObject()
{
}
FrogEnemyObject::~FrogEnemyObject()
{
	if(tongueString)
		delete(tongueString);
	tongueString = NULL;
}
bool FrogEnemyObject::GetFreeRotate()
{
	return false;
}
Vector2D * FrogEnemyObject::GetWallPoseCoordinates()
{
	Vector2D * texCoord = (Vector2D *)malloc(4 * sizeof(Vector2D));
	texCoord[0] = Vector2D(0,1);
	texCoord[1] = Vector2D(1, 1);
	texCoord[2] = Vector2D(1, 0);
	texCoord[3] = Vector2D(0, 0);
	return texCoord;
}
void FrogEnemyObject::ReleasingObject()
{
	canUseTongue = true;	
}
void FrogEnemyObject::PickingUpObject()
{
	canUseTongue = false;
	if(tongueString->grabbingObject)
	{
		tongueString->Release();
	}
	health = 0;
}
bool FrogEnemyObject::TongueCollide()
{


	Vector2D * location = new Vector2D(0,0);
	if(tongueString->Collide(target, location))
	{

		printf("LICKED!\n");
		target->AddKeyPoint(location);
		tongueString->Grab(target, location);
		tongueString->SetSpringConstant(LOOSE_TONGUE);
		return true;	
	}
	return false;
}
void FrogEnemyObject::Wander(double elapsedTime)
{
	Vector2D direction;
	
	
	if(isOnGround)
	{
		direction = Vector2D(0,1);
	}
	else if(isOnRightWall)
	{
		direction = Vector2D(1,0);
	}
	else if(isOnLeftWall)
	{
		direction = Vector2D(-1,0);
	}
	else
		return;

		float multiplier = velocity.Magnitude() * HOP_MULTIPLIER;
		if(multiplier > MAX_HOP_FORCE)
			multiplier = MAX_HOP_FORCE;
		else if(multiplier < abs(GRAVITATIONAL_FORCE) * mass)
			multiplier = abs(GRAVITATIONAL_FORCE) * mass;
		AddForce(direction * multiplier * mass);
}
bool FrogEnemyObject::AttackThink(double elapsedTime)
{
	

	if(weapon)
	{
		//printf("Calling weapon think\n");
		weapon->Think(elapsedTime, target, isOnGround);
	}
	if(tongueString)
	{
		tongueAnchor = position + tongueAnchorOffset;
		Vector2D force = tongueString->Think(elapsedTime);
		if(tongueString->grabbingObject)
		{
			float tongueLength = tongueString->GetLength();
	
			if(tongueLength > 30)
			{
				tongueString->Release();
				tongueString->SetSpringConstant(TIGHT_TONGUE);
			}
			else
				AddForce(0.2 * force);
				
		}
		else
		{
			if(canUseTongue && position.dist(target->position) < 30.0)
			{
				tongueString->DirectionalWhip(target->position -position);
			}
			TongueCollide();
		}
	}
	return false;
}
void FrogEnemyObject::CpuControlledObjectDrawEntryPoint()
{
	if(tongueString)
	{
		tongueString->Draw();
	}
}
void FrogEnemyObject::MoveRight()
{
	Vector2D right = Vector2D(1,5);
	right = !right;

	float rightSpeed = right.dot(velocity);
	if(rightSpeed > 500)
		return;
	movingRight = true;
	isOnLeftWall = false;
	if(isOnGround)
	{
		float multiplier = velocity.Magnitude() * HOP_MULTIPLIER;
		if(multiplier > MAX_HOP_FORCE)
			multiplier = MAX_HOP_FORCE;
		else if(multiplier < abs(GRAVITATIONAL_FORCE) * mass)
			multiplier = abs(GRAVITATIONAL_FORCE) * mass;
		AddForce(right * multiplier * mass);
	}
}
void FrogEnemyObject::MoveLeft()
{
	isOnRightWall = false;
	Vector2D left = Vector2D(-1,5);
	left = !left;
	float leftSpeed = left.dot(velocity);
	if(leftSpeed > 500)
		return;
	movingLeft = true;
	if(isOnGround)
	{
		float multiplier = velocity.Magnitude() * HOP_MULTIPLIER;
		if(multiplier > MAX_HOP_FORCE)
			multiplier = MAX_HOP_FORCE;
		else if(multiplier < abs(GRAVITATIONAL_FORCE) * mass)
			multiplier = abs(GRAVITATIONAL_FORCE) * mass;
		AddForce(left * multiplier * mass);
	}
}
